package org.aplikator.client.shared.descriptor;

import org.aplikator.client.local.panels.ContentsPanel;
import org.aplikator.client.local.widgets.HasFields;
import org.aplikator.client.local.widgets.TableFactory;
import org.aplikator.client.local.widgets.TableInterface;

import com.google.gwt.user.client.ui.Widget;
import org.jboss.errai.common.client.api.annotations.Portable;

@SuppressWarnings("serial")
//@JsonTypeName("table")
//@Portable
public class TableDTO extends WidgetPropertyDTOBase implements RenderWidgetAware, ContainsViewDTOReference {

	public static final String TYPE_KEY = "table";
	
    private ViewDTO view;
    private boolean sref;
    
    @SuppressWarnings("unused")
    public TableDTO() { //TODO custom marshaller
    }

    public TableDTO(PropertyDTO property, ViewDTO view) {
        super(property);
        this.view = view;
    }

    
    public ViewDTO getView() {
		return view;
	}
    
    public void setView(ViewDTO view) {
		this.view = view;
	}

    
    
    @Override
	public boolean isSelfReferenceFlag() {
    	return this.sref;
    }

	@Override
	public void setSelfReferenceFlag(boolean sref) {
		this.sref = sref;
	}

	@Override
    public Widget getWidget(HasFields form) {
        ContentsPanel frame = new ContentsPanel();
        TableInterface table = TableFactory.create(view, property, form);
        table.setEnabled(isEnabled());
        form.registerNestedCollection(table);
        frame.setWidget(table.asWidget());
        frame.setTitle(view.getLocalizedName());
        frame.setHeight("" + (6 + 2 * (view.getPageSize() > 0 ? view.getPageSize() : 15)) + "em");// TODO
                                                                                                                // nahradit
                                                                                                                // 15
                                                                                                                // za
                                                                                                                // DEFAULTPAGESIZE,
                                                                                                                // po
                                                                                                                // jejim
                                                                                                                // presunuti
                                                                                                                // do
                                                                                                                // konstant
        frame.setWidth("100%");// TODO doplnit podporu explicitni sirky
        return frame;
    }

}
